package krow.dev.gunpobus.control.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import krow.dev.gunpobus.Constants.Attribute;
import krow.dev.gunpobus.Constants.Value;
import krow.dev.gunpobus.model.NodeItem;
import krow.dev.gunpobus.model.NodeItem.NodeType;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.TextExtractor;

public class NodeParser implements IHtmlParser {

	private static Pattern sStationIdPattern = Pattern.compile("(?:stationId=([0-9]+))");

	private List<NodeItem> mNodeItemList;

	private NodeItem.NodeType mNodeType;

	public NodeParser() {

		mNodeItemList = new ArrayList<NodeItem>();

		mNodeType = NodeType.First_Half;
	}

	public List<NodeItem> getNodeItemList() {

		return mNodeItemList;
	}

	@Override
	public void parse(Source source) throws Exception {

		List<Element> tableElemeList = source.getAllElements(HTMLElementName.TABLE);
		for (Element tableElement : tableElemeList) {
			if (!Value.CLASS_NODEBOX.equals(tableElement.getAttributeValue(Attribute.CLASS))) {
				continue;
			}

			List<Element> nodeElementList = tableElement.getAllElements(HTMLElementName.TR);
			for (Element nodeElement : nodeElementList) {
				parseNode(nodeElement);
			}
		}
		
		int size = mNodeItemList.size();
		if(size >= 2){
			NodeType nodeType = NodeType.End;
			if(mNodeType == NodeType.First_Half){
				nodeType = NodeType.End_No_Return;
			}
			mNodeItemList.get(size - 1).setNodeType(nodeType);
		}
	}

	private void parseNode(Element element) {
		String id = null;
		String name = null;
		boolean hasBus = false;
		NodeItem.NodeType nodeType = mNodeType;

		try {

			List<Element> dataElementList = element.getAllElements(HTMLElementName.TD);
			for (Element dataElement : dataElementList) {
				String reference = dataElement.getAttributeValue(Attribute.CLICK);
				if (reference == null) {
					continue;
				}
				Matcher matcher = sStationIdPattern.matcher(reference);
				if (matcher.find()) {
					id = matcher.group(1);

					List<Element> spanElementList = dataElement.getAllElements(HTMLElementName.SPAN);
					for (Element spanElement : spanElementList) {
						String className = spanElement.getAttributeValue(Attribute.CLASS);
						if (className != null && className.equals("fonttype1")) {
							TextExtractor textextractor = spanElement.getTextExtractor();
							if (textextractor != null) {
								name = spanElement.getTextExtractor().toString();
							}
						}
					}
				}
			}
			if (id == null) {
				return;
			}

			List<Element> imageElementList = element.getAllElements(HTMLElementName.IMG);
			for (Element imageElement : imageElementList) {
				String reference = imageElement.getAttributeValue(Attribute.SOURCE);
				if (reference == null) {
					continue;
				}

				if (reference.indexOf("ico_bus") > 0) {
					hasBus = true;
				} else if (reference.indexOf("ico_start") > 0) {
					nodeType = NodeType.Start;
					mNodeType = NodeType.First_Half;
				} else if (reference.indexOf("ico_return") > 0) {
					nodeType = NodeType.Retrun;
					mNodeType = NodeType.Second_Half;
				} else if (reference.indexOf("ico_end") > 0) {
					nodeType = NodeType.End;
				}
			}
		} catch (Exception e) {
		}

		NodeItem nodeItem = new NodeItem();
		nodeItem.setId(id);
		nodeItem.setName(name);
		nodeItem.setHasBus(hasBus);
		nodeItem.setNodeType(nodeType);

		mNodeItemList.add(nodeItem);
	}
}
